package com.example.task1.ui.AllService.parking;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.ParkingCartBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class ParkingModificationCartActivity extends BaseActivity {

    private int id;
    private EditText tvNum;
    private EditText tvType;
    private Button btnSave;
    private ParkingCartBean.RowsDTO cartDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_modification_cart);
        initView();
        if (id == 0) {
            cartDate = MyApplication.Parking.CartDate;
            tvNum.setText(cartDate.getPlateNo());
            tvType.setText(cartDate.getType());
        }
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = tvNum.getText().toString();
                String type = tvType.getText().toString();
                if (TextUtils.isEmpty(num)) {
                    tvNum.setError("不能为空");
                }else if (TextUtils.isEmpty(type)) {
                    tvType.setError("不能为空");
                }else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("plateNo",num);
                    map.put("type",type);
                    if (id == 0) {
                        map.put("id",cartDate.getId());
                        Call<ApiResp> put = Api.api().put(MyApplication.TOKEN, "/prod-api/api/park/car", Api.getBody(map));
                        Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                MyApplication.show("修改成功");
                                finish();
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    }else {
                        Call<ApiResp> put = Api.api().post(MyApplication.TOKEN, "/prod-api/api/park/car", Api.getBody(map));
                        Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                MyApplication.show("新增成功");
                                finish();
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    }

                }
            }
        });
    }

    @Override
    public String getMyTitle() {
        id = getIntent().getIntExtra("id", 0);
        if (id == 0) {
            return "修改车辆信息";
        }
        return "保存车辆信息";
    }

    private void initView() {
        tvNum = findViewById(R.id.tv_num);
        tvType = findViewById(R.id.tv_type);
        btnSave = findViewById(R.id.btn_save);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.Parking.CartDate = null;
    }
}